﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.IO;

using NUnit.Framework;

using Refix.Core.Commands;

namespace Refix.Core.Tests.Commands
{
    [TestFixture]
    public class GetTest : TestSolutionFolderTest
    {
        [Test]
        public void Validate_Insists_On_Arguments()
        {
            var queue = new Queue<string>();
            queue.Enqueue("get");
            var get = new CommandWithOptions(queue);

            Assert.False(get.Command.Validate());
        }

        [Test]
        public void Validate_Passes_For_Single_Assembly_Version()
        {
            var queue = new Queue<string>();
            queue.Enqueue("get");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.4");
            var get = new CommandWithOptions(queue);

            Assert.True(get.Command.Validate());
        }

        [Test]
        public void Execute_Gets_Default_Runtime_Version()
        {
            string tempPath = Path.GetTempPath();
            var file = new FileInfo(Path.Combine(tempPath, "Refix.Test.A.dll"));
            if (file.Exists)
            {
                file.Delete();
            }

            Environment.CurrentDirectory = tempPath;
            var queue = new Queue<string>();
            queue.Enqueue("get");
            queue.Enqueue("-fr");
            queue.Enqueue(Path.Combine(SolutionRootDirectory.FullName, ".testrefixrepo"));
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.4");
            var command = new CommandWithOptions(queue);
            command.Command.Validate();
            command.Command.Execute();

            file.Refresh();
            Assert.True(file.Exists, "File was not copied locally.");
            Assert.AreEqual(4608, file.Length, "Wrong version of DLL was fetched - should have been default.");
            file.Delete();
        }

        [Test]
        public void Execute_Gets_Specified_Runtime_Version_If_Exists()
        {
            string tempPath = Path.GetTempPath();
            var file = new FileInfo(Path.Combine(tempPath, "Refix.Test.A.dll"));
            if (file.Exists)
            {
                file.Delete();
            }

            Environment.CurrentDirectory = tempPath;
            var queue = new Queue<string>();
            queue.Enqueue("get");
            queue.Enqueue("-frc");
            queue.Enqueue(Path.Combine(SolutionRootDirectory.FullName, ".testrefixrepo"));
            queue.Enqueue("net-4.0");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.4");

            var command = new CommandWithOptions(queue);
            command.Command.Validate();
            command.Command.Execute();

            file.Refresh();
            Assert.True(file.Exists, "File was not copied locally.");
            Assert.AreEqual(73216, file.Length, "Wrong version of DLL was fetched - should have been .NET 4.0.");
            file.Delete();
        }

        [Test]
        public void Execute_Gets_Default_Runtime_Version_If_Specified_Does_Not_Exist()
        {
            string tempPath = Path.GetTempPath();
            var file = new FileInfo(Path.Combine(tempPath, "Refix.Test.A.dll"));
            if (file.Exists)
            {
                file.Delete();
            }

            Environment.CurrentDirectory = tempPath;
            var queue = new Queue<string>();
            queue.Enqueue("get");
            queue.Enqueue("-frc");
            queue.Enqueue(Path.Combine(SolutionRootDirectory.FullName, ".testrefixrepo"));
            queue.Enqueue("net-5.0");
            queue.Enqueue("Refix.Test.A");
            queue.Enqueue("0.1.0.4");

            var command = new CommandWithOptions(queue);
            command.Command.Validate();
            command.Command.Execute();

            file.Refresh();
            Assert.True(file.Exists, "File was not copied locally.");
            Assert.AreEqual(4608, file.Length, "Wrong version of DLL was fetched - should have been .NET 4.0.");
            file.Delete();
        }
    }
}